package EP_XSL_Extensions::EPC_DD::DD;
use strict;

######################################################################
# Time-stamp: <03/09/25 13:05:11 ostolop>
#
# General Dynamic Data Routines and Structures
######################################################################

use XML::LibXML;

sub session_id {
    my ($query) = new CGI;

    my %cookie = $query->cookie($Config::h->{SESSION_COOKIE_NAME} );
    return $cookie{session_id};
}

my $dynamic_data_XML = <<__DynXML__;
<?xml version="1.0"?><dynamic_data/>
__DynXML__

sub dynamic_list {
  my ($list) = @_;

  my $doc;
  my $parser = XML::LibXML->new();
  $doc = $parser->parse_string ( $dynamic_data_XML );

  my $root = $doc->getDocumentElement();

  foreach ( sort { $a <=> $b } keys %$list ) {
    $root->appendChild ( create_dynamic_list_item ( $doc, $list->{$_} ) );
  }

  return $doc;
}

sub create_dynamic_list_item {
  my ($doc, $list_item_hash) = @_;
  my ($value_select);

  my $list_item = $doc->createElement ( "item" );

  foreach my $item_attr_name ( keys %$list_item_hash ) {
    # for value put it into text
    if ( $item_attr_name =~ /value/) {
      my $value_text = $doc->createTextNode ( $list_item_hash->{value} );
      $list_item->appendChild ( $value_text );
      next;
    }

    my $item_attr_value = "";

    $_ = ref ( $list_item_hash->{$item_attr_name} );

    if (not $_) {
      $item_attr_value = $list_item_hash->{$item_attr_name};
    } else {
    SWITCH: {
	/ARRAY/
	  && do {
	    $item_attr_value = join ( "; ", @{$list_item_hash->{$item_attr_name}} );
	};
	
	/HASH/
	  && do {
	    foreach ( keys %{$list_item_hash->{$item_attr_name}} ) {
	      $item_attr_value .= "; " if not $item_attr_value eq "";
	      $item_attr_value .= "$_: ".$list_item_hash->{$item_attr_name}->{$_};
	    };
	  }
	}
    }

    $list_item->setAttribute ( $item_attr_name, $item_attr_value );
  }

  return $list_item;
}

1;

